/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.saving;

import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.gl.Glaccnt;
import src.domain.saving.Savtrxgl;
import src.services.gl.GlaccntConverter;
import src.services.saving.SavtrxglModel;
import src.util.Constanta;
import src.util.Messager;

/**
 *
 * @author jay
 */

@ManagedBean
@SessionScoped
public class SavtrxglBean implements Serializable {

    private List<Savtrxgl> savtrxgls;
    private List<SelectItem> avblName;
    private List<Glaccnt> avblGL;
    private Savtrxgl selectedData, unsaveData;
    private SavtrxglModel helper;
    private boolean editMode=true;

    public SavtrxglBean() {
        this.avblName = Constanta.getSavtrxGl();
        this.helper = new SavtrxglModel();
        this.savtrxgls = helper.findAll(null);
        this.avblGL = GlaccntConverter.glaccountDB;
        this.selectedData = null;
        this.unsaveData = null;
    }

    public List<Savtrxgl> getSavtrxgls() {
        return savtrxgls;
    }
    
    
    public Savtrxgl getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Savtrxgl selectedData) {
        this.selectedData = selectedData;
    }

    public Savtrxgl getUnsaveData() {
        return unsaveData;
    }

    public void setUnsaveData(Savtrxgl unsaveData) {
        this.unsaveData = unsaveData;
    }

    public List<Glaccnt> getAvblGL() {
        return avblGL;
    }

    public List<SelectItem> getAvblName() {
        return avblName;
    }
       
   // public String selectItemValueOf(Integer id) {
   //     return (String) Utils.selectItemValueOf(avblGL, id);
   // }
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.unsaveData = null;
    }
    
    //public void handleGLSelected(SelectEvent event) {
    //    unsaveData.setGlaccount((Glaccnt) event.getObject());
    //} 
    
    public void initForm() {
        this.avblGL = GlaccntConverter.glaccountDB;
    }
    
    public void openData() { 
        initForm();
        this.editMode = true;
        this.unsaveData = Savtrxgl.clone(selectedData);
    }
    
    public void newData() {  
        initForm();
        this.selectedData = null;
        this.unsaveData = new Savtrxgl();
        this.editMode = false;
    }
    
    public void save() {
        RequestContext context = RequestContext.getCurrentInstance();  
     
        boolean success = helper.saveData(this.unsaveData);
        if (success==true) {
            if (this.editMode==false) this.savtrxgls.add(unsaveData);
            else {
                int i = savtrxgls.indexOf(selectedData);
                if (i <= savtrxgls.size()) savtrxgls.set(i, Savtrxgl.clone(unsaveData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) unsaveData.setSavtrxglId(null);
        } 
                        
        Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success = helper.deleteData(this.selectedData);
        
        if (success) {
            this.savtrxgls.remove(this.selectedData);
            this.selectedData = null;
        }
               
        Messager.showDeleteStatus(success);
        context.addCallbackParam("success", success);  
    }
    
}
